import gantt from 'public/3thComponents/gantt' ; // 引入模块
import 'public/3thComponents/ganttExport/ganttExport.js';
import 'public/3thComponents/gantt/codebase/dhtmlxgantt.css';
import layout from './parts/layout';
import tools from "../../../tools";
export default class taskModel {
    constructor(initConfig) {
        this.tools=tools;
        this.gantt=gantt;
        this.initConfig =initConfig;
        this.config={};
        this.template=null;
        this.setConfig();
        this.gantt.config={...this.gantt.config,...this.config};
        gantt.init(this.initConfig.el);
    }
    static create(initConfig=taskModel.getInitConfig()){
        let object=new taskModel(Object.assign(taskModel.getInitConfig(),initConfig));
        return object;
    }
    static getInitConfig(){
        return {
            el:null,
            columns:true,
            // scalesPattern:["year","month","date"],
        }
    }
    setConfig(){
        this.config={
            layout:layout.defaultLayout(),
            columns:this.setColumns(),
            scales:this.setScales(),
            scale_height:50,
            // autosize: true,
            /*date格式*/
            date_format:"%Y-%m-%d %H:%i:%s",
            date_scale: "%Y-%m-%d",
            time_format: ["%Y", "%m", "%d"],
            /*附加标识线*/
            marker:true,
            autofit:true,
            autoscroll:true,
            autoscroll_speed:50,
            /*project 合并后依然具有收起图标*/
            open_split_tasks:true,
            /*auto scheduling*/
            // auto_scheduling:true,
            /*drag-drop*/
            order_branch:'marker',
            order_branch_free:true,
            /*显示评估和可能区间*/
            highlight_critical_path:true,
            show_slack:true,
            /*设置工作时间*/
            work_time:true,
            duration_unit:'day',
            /*task操作*/
            cascade_delete:true,
            /*列头排序操作*/
            sort:true,
            group:true,
            task_date: "%Y-%F-%d",
            buttons_left: ['gantt_delete_btn'],
            buttons_right: ['gantt_save_btn', 'gantt_cancel_btn'],
            // lightbox: {
            //     sections: [
            //         [
            //             {
            //                 "name": "description",
            //                 "height": 70,
            //                 "map_to": "text",
            //                 "type": "textarea",
            //                 "focus": true,
            //                 "id": "area_1692952903491",
            //                 "inputId": "input_1692952903493"
            //             },
            //             {
            //                 "name": "time",
            //                 "type": "duration",
            //                 "map_to": "auto",
            //                 "id": "area_1692952903492",
            //                 "_time_format_order": {
            //                     "0": 0,
            //                     "1": 1,
            //                     "2": 2,
            //                     "size": 3
            //                 },
            //                 "inputId": "input_1692952903494"
            //             },
            //         ]
            //     ]
            // },
            // auto_scheduling:true,
            // auto_scheduling_strict:true,
            // auto_scheduling_initial:true,
            // auto_scheduling_compatibility:true,
            // auto_scheduling_descendant_links:true,
            refresh:true,
        }
        // this.config.scale_height=this.initConfig.scalesPattern.length*25<50?50:this.initConfig.scalesPattern.length*25;
    }
    setColumns(){
        let columns=this.initColumns();
        if(!!this.initConfig.columns&&typeof(this.initConfig.columns)==='object'){
            Object.keys(this.initConfig.columns).map(key=>{
                if(Object.keys(columns).includes(key)){
                    if(typeof(this.initConfig.columns[key])==='boolean'){
                        columns[key]=Object.assign(columns[key],{hide:!this.initConfig.columns[key]});
                    }else if(typeof(this.initConfig.columns[key])==='object'){
                        columns[key]=this.initConfig.columns[key];
                    }
                }else{
                    if(typeof(this.initConfig.columns[key])==='object'){
                        columns[key]=this.initConfig.columns[key];
                    }
                }
            })
        };
        return Object.values(columns).sort((a,b)=>a.index-b.index);
    }
    initColumns(){
        return {
            wbs:{index:0,name:"wbs",label:"序号",resize:true,width:40, template:gantt.getWBSCode},
            text:{index:1,name:"text",label:"任务名称",tree:true,resize:true,width:200},
            // holder:{index:2,name:"holder",label:"负责人",resize:true,width:"*"},
            // workunitName:{index:3,name:"workunitName",label:"工位名称",resize:true,width:"*"},
            // workunit:{index:4,name:"workunit",label:"工位ID",resize:true,width:"*",hide:true},
            // constraint_type:{index:5,name:"constraint_type",label:"约束",hide:true,resize:true,width:"*",align:"center"},
            // constraint_date:{index:6,name:"constraint_date",label:"约束时间",hide:true,resize:true,width:"*",align:"center"},
            planned_start:{index:7,name:"planned_start",label:"计划开始时间",hide:false,resize:true,width:100,align:"center"},
            planned_end:{index:8,name:"planned_end",label:"计划结束时间",hide:false,resize:true,width:100,align:"center"},
            start_date:{index:9,name:"start_date",label:"实际开始时间",resize:true,width:100,align:"center"},
            end_date:{index:10,name:"end_date",label:"实际结束时间",hide:false,resize:true,width:100,align:"center"},
            duration:{index:11,name:"duration",label:"期间",resize:true,width:40,align:"center"},
            progress:{index:12,name:"progress",label:"进度",resize:true,width:60,align:"center",template:item => {
                let num = (item.progress*100).toFixed(1);
                return num+'%';
            }},
            add:{index:100,name:"add",label:"",width:44}
        }
    }
    setScales(){
        if(!this.initConfig.scalesPattern){
            this.initConfig.scalesPattern=['yearmonthdate'];
        }
        return Object.values(this.initScales()).filter(item=>this.initConfig.scalesPattern.includes(item.type))
    }
    initScales(){
        let _this=this;
        return {
            year:{type:'year',unit: "year", step: 1, format: function (date) {return _this.tools.formatter.year(date,"%YY")}},
            quarter:{type:'quarter',unit: "quarter", step: 1, format: function(date){return "Q#"+_this.tools.formatter.quarter(date,"%EQ")}},
            yearquarter:{type:'yearquarter',unit: "quarter", step: 1, format: function (date) {return _this.tools.formatter.year(date,"%YY")+" Q#"+_this.tools.formatter.quarter(date,"%EQ")}},
            month:{type:'month',unit: "month", step: 1, format: function (date) {return _this.tools.formatter.month(date,"%EM")}},
            yearmonth:{type:'yearmonth',unit: "month", step: 1, format: function (date) {return _this.tools.formatter.year(date,"%YY")+"-"+_this.tools.formatter.month(date,"%EM")}},
            yearquartermonth:{type:'yearquartermonth',unit: "month", step: 1, format: function (date) {return _this.tools.formatter.year(date,"%YY")+" Q#"+_this.tools.formatter.quarter(date,"%EQ")+"-"+_this.tools.formatter.month(date,"%EM")}},
            quartermonth:{type:'quartermonth',unit: "month", step: 1, format: function (date) {return " Q#"+_this.tools.formatter.quarter(date,"%EQ")+"-"+_this.tools.formatter.month(date,"%EM")}},
            week:{type:'week',unit: "week", step: 1, format: function (date) {return "Week #" + this.gantt.date.getWeek(date)}},
            workday:{type:'workday',unit: "day", step: 1, format: "%D", css: function(date){
                    let dateToStr = this.gantt.date.date_to_str("%D");
                    if (dateToStr(date) == "Sun"||dateToStr(date) == "Sat"){
                        return "nonworkday";
                    }else{
                        return "";
                    }
                }
            },
            day:{type:'day',unit: "day", step: 1, format: function(date) {return _this.tools.formatter.day(date,"%EN")}},
            date:{type:'date',unit: "day", step: 1, format: function (date) {return _this.tools.formatter.date(date,"%DD")}},
            daydate:{type:'daydate',unit: "day", step: 1, format: function (date) {return _this.tools.formatter.date(date,"%DD")+","+_this.tools.formatter.day(date,"%EN")}},
            yearmonthdate:{type:'yearmonthdate',unit: "day", step: 1, format: function (date) {return _this.tools.formatter.year(date,"%YY")+"-"+_this.tools.formatter.month(date,"%EM")+"-"+_this.tools.formatter.date(date,"%DD")}},
            hour:{type:'hour',unit: "hour", step: 1, format: function(date) {return _this.tools.formatter.hour(date,"%HH")}},
            minute:{type:'minute',unit: "minute", step: 1, format: function(date) {return _this.tools.formatter.minute(date,"%MM")}},
        }
    }
}